package haxeframe.sound;

import haxeframe.sound.SoundManager;
import flash.events.Event;
import flash.media.SoundChannel;

class Sequence
{
	private var _sounds:Array<String>;
	private var _currentSound:Int = 0;
	private var _channel:SoundChannel;
	private var _channelName:String;
	private var _loop:Bool;

	public function new(sounds:Array<String>, channel:String=null, loop:Bool=false)
	{
		if(channel != null) _channelName = channel;
		_sounds = sounds;
		_loop = loop;
	}

	public function play(startSound:Int=0):Void
	{
		_currentSound = startSound;
		_channel = SoundManager.playSound(_sounds[_currentSound],0,0,_channelName);
		if(_channel != null)
			_channel.addEventListener( Event.SOUND_COMPLETE, onSoundComplete);
	}

	private function onSoundComplete(event:Event):Void
	{
		++_currentSound;
		if(_currentSound < _sounds.length){
			play(_currentSound);
			return;
		}
		if(_loop){
			play();
		}
	}
}